package com.example.controller;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSON;
import com.example.consts.RbmConst;
import com.example.service.PostProblemService;
import com.example.service.ProblemCaseService;
import com.example.service.ProblemService;
import com.example.util.JsonUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description //TODO
 * @Date 2022/9/15 19:46
 * @Author lonng
 **/
@ResponseBody
@Controller
@RequestMapping("doProblem")
public class DoProblemController {
    private static Integer Version=1;
    @Autowired
    private ProblemService problemService;

    @Autowired
    private PostProblemService postProblemService;

    @Autowired
    private JsonUtil jsonUtil;

    @Autowired
    private ProblemCaseService problemCaseService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("{id}")
    public String gitById(@PathVariable("id")Integer id){
        return jsonUtil.getJson(problemService.getById(id));
    }

    @GetMapping("sendProblem")
    public String postProblem(@RequestParam("language")String language,@RequestParam("requestBody")String requestBody,@RequestParam("timeLimit")Integer timeLimit, @RequestParam("memoryLimit")Integer memoryLimit){
//        System.out.println(language);
//        System.out.println(requestBody);
//        System.out.println(timeLimit);
//        System.out.println(memoryLimit);
//        System.out.println(postProblemService.sendProblem(language,requestBody,timeLimit,memoryLimit));
        return  postProblemService.sendProblem(language,requestBody,timeLimit,memoryLimit);

    }

    @GetMapping ("evaluating")
    public String evaluatingProblem(@RequestParam("language")String language,@RequestParam("requestBody")String requestBody,
                                    @RequestParam("timeLimit")Integer timeLimit, @RequestParam("memoryLimit")Integer memoryLimit,
                                    @RequestParam("stackLimit")Integer stackLimit,@RequestParam("pid")Integer pid,HttpServletRequest request){
        System.out.println(request.getAttribute("uid"));
        String a= postProblemService.sendProblem(language,requestBody,timeLimit,memoryLimit);
        JSONArray jsonArray = null;
        jsonArray = new JSONArray(a);
        String fileIds="";
        if(language.equals("Java")) {
            fileIds = jsonArray.getJSONObject(0).get("fileIds").toString().replace("\"Main.java\":", "").replace("{\"", "").replace("\"}", "");
        }else{
            String pattern = "\"Main.cc\".*:";
            System.out.println("C++");
            fileIds=jsonArray.getJSONObject(0).get("fileIds").toString().replaceAll(pattern,"").replace("\"}","").replace("{\"","");
        }
        String result=postProblemService.evaluatingProblem(language,fileIds,timeLimit,memoryLimit,stackLimit,problemCaseService.findAllByPid(pid).get(0).getInput());
        rabbitTemplate.convertAndSend(RbmConst.Judge_QUEUE,problemService.getMap(result,requestBody,Version,pid,language,String.valueOf(request.getAttribute("uid"))));

        return result;
    }
    @GetMapping ("run")
    public String runProblem(@RequestParam("language")String language,@RequestParam("requestBody")String requestBody,
                                    @RequestParam("timeLimit")Integer timeLimit, @RequestParam("memoryLimit")Integer memoryLimit,
                                    @RequestParam("stackLimit")Integer stackLimit,@RequestParam("pid")Integer pid,HttpServletRequest request){
        String a= postProblemService.sendProblem(language,requestBody,timeLimit,memoryLimit);
        JSONArray jsonArray = null;
        jsonArray = new JSONArray(a);
        String fileIds="";
        if(language.equals("Java")) {
            fileIds = jsonArray.getJSONObject(0).get("fileIds").toString().replace("\"Main.java\":", "").replace("{\"", "").replace("\"}", "");
        }else{
            String pattern = "\"Main.cc\".*:";
//            System.out.println("C++");
            fileIds=jsonArray.getJSONObject(0).get("fileIds").toString().replaceAll(pattern,"").replace("\"}","").replace("{\"","");
        }
        int num=0;
        System.out.println(problemCaseService.findAllByPid(pid).size());
        for(int i=0;i<problemCaseService.findAllByPid(pid).size();i++) {
            String result="";
            result = postProblemService.evaluatingProblem(language, fileIds, timeLimit, memoryLimit, stackLimit, problemCaseService.findAllByPid(pid).get(i).getInput());
            System.out.println(result);
            JSONArray jsonArray2 = null;
            jsonArray2 = new JSONArray(result);
            String pattern2 = "\",\"stderr\".*";
            String outMessage=jsonArray2.getJSONObject(0).get("files").toString().replaceAll(pattern2,"").replace("\"}","").replace("{\"stdout\":\"","").replace("\\n","").replaceAll("\\p{C}", "");
            System.out.println(outMessage);
            System.out.println(problemCaseService.findAllByPid(pid).get(i).getOutput().replaceAll("\\p{C}", ""));
            if(outMessage.equals(problemCaseService.findAllByPid(pid).get(i).getOutput().replaceAll("\\p{C}", ""))){
                num=num+1;
            }
        }
        return num+"/"+problemCaseService.findAllByPid(pid).size();
    }

}
